package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DoctorMapper;
import com.kkb.mapper.HosregisterMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.pojo.Hosregister;
import com.kkb.pojo.HosregisterExample;
import com.kkb.vo.RegistrationQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RegistrationService {

    @Resource
    private HosregisterMapper hosregisterMapper;
    @Resource
    private DoctorMapper doctorMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<Hosregister> queryByPage(Integer pageNum, Integer pageSize, RegistrationQueryVO vo) {
        HosregisterExample hosregisterExample = new HosregisterExample();
        DoctorExample doctorExample = new DoctorExample();
        HosregisterExample.Criteria criteria = hosregisterExample.createCriteria();
        DoctorExample.Criteria criteria1 = doctorExample.createCriteria();
        if (vo != null) {
            if (vo.getId() != null) {
                criteria.andIdEqualTo(vo.getId());
            }
            boolean flag = false;
            if (vo.getDoctorName() != null && !"".equals(vo.getDoctorName().trim())) {
                criteria1.andNameEqualTo(vo.getDoctorName().trim());
                flag = true;
            }
            if (vo.getKeShi() != null && !"".equals(vo.getKeShi().trim())) {
                criteria1.andKeShiEqualTo(vo.getKeShi().trim());
                flag = true;
            }
            if (flag) {
                List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
                List<Integer> idList = new ArrayList<>();
                for (Doctor doctor : doctors) {
                    idList.add(doctor.getId());
                }
                criteria.andDoctorIdIn(idList);
            }
            if (vo.getBeginDate() != null) {
                criteria.andDateGreaterThan(vo.getBeginDate());
            }
            if (vo.getEndDate() != null) {
                criteria.andDateLessThan(vo.getEndDate());
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Hosregister> hosregisters = hosregisterMapper.selectByExample(hosregisterExample);
        for (Hosregister hosregister : hosregisters) {
            hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getDoctorId()));
        }
        return new PageInfo<>(hosregisters);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Hosregister queryById(Integer id) {
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(id);
        hosregister.setDoctor(doctorMapper.selectByPrimaryKey(hosregister.getDoctorId()));
        return hosregister;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateRegister(Hosregister hosregister) {
        String doctorName = "";
        if ("0".equals(hosregister.getDoctorName().trim()))
            doctorName = "程俊";
        else if ("1".equals(hosregister.getDoctorName().trim()))
            doctorName = "欧阳雨露";
        else if ("2".equals(hosregister.getDoctorName().trim()))
            doctorName = "王博";
        else if ("3".equals(hosregister.getDoctorName().trim()))
            doctorName = "沈青川";
        String keShi = "";
        if ("0".equals(hosregister.getKeShi().trim()))
            keShi = "急诊科";
        else if ("1".equals(hosregister.getKeShi().trim()))
            keShi = "骨科";
        else if ("2".equals(hosregister.getKeShi().trim()))
            keShi = "血液科";
        else if ("3".equals(hosregister.getKeShi().trim()))
            keShi = "外科";
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        criteria.andNameEqualTo(doctorName);
        criteria.andKeShiEqualTo(keShi);
        List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
        if (doctors.size() == 0 || doctors == null) {
            return -1;
        } else {
            hosregister.setDoctorId(doctors.get(0).getId());
        }
        return hosregisterMapper.updateByPrimaryKeySelective(hosregister);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteRegister(Integer id) {
        Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(id);
        hosregister.setState(3);
        return hosregisterMapper.updateByPrimaryKeySelective(hosregister);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addRegister(Hosregister hosregister) {
        String doctorName = "";
        if ("0".equals(hosregister.getDoctorName().trim()))
            doctorName = "程俊";
        else if ("1".equals(hosregister.getDoctorName().trim()))
            doctorName = "欧阳雨露";
        else if ("2".equals(hosregister.getDoctorName().trim()))
            doctorName = "王博";
        else if ("3".equals(hosregister.getDoctorName().trim()))
            doctorName = "沈青川";
        String keShi = "";
        if ("0".equals(hosregister.getKeShi().trim()))
            keShi = "急诊科";
        else if ("1".equals(hosregister.getKeShi().trim()))
            keShi = "骨科";
        else if ("2".equals(hosregister.getKeShi().trim()))
            keShi = "血液科";
        else if ("3".equals(hosregister.getKeShi().trim()))
            keShi = "外科";
        DoctorExample doctorExample = new DoctorExample();
        DoctorExample.Criteria criteria = doctorExample.createCriteria();
        criteria.andNameEqualTo(doctorName);
        criteria.andKeShiEqualTo(keShi);
        List<Doctor> doctors = doctorMapper.selectByExample(doctorExample);
        if (doctors.size() == 0 || doctors == null) {
            return -1;
        } else {
            hosregister.setDoctorId(doctors.get(0).getId());
            hosregister.setState(0);
            hosregister.setDate(new Date());
        }
        return hosregisterMapper.insertSelective(hosregister);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteAllRegister(ArrayList<Integer> ids) {
        for (int i = 0; i < ids.size(); i++) {
            if (hosregisterMapper.selectByPrimaryKey(ids.get(i)).getState() != 0) {
                return -1;
            }
        }
        int count = 0;
        for (int i = 0; i < ids.size(); i++) {
            Hosregister hosregister = hosregisterMapper.selectByPrimaryKey(ids.get(i));
            hosregister.setState(3);
            hosregisterMapper.updateByPrimaryKeySelective(hosregister);
            count++;
        }
        return count;
    }
}
